package com.moetube.video.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.moetube.entity.PageResult;
import com.moetube.entity.Result;
import com.moetube.entity.StatusCode;
import com.moetube.video.pojo.Barrage;
import com.moetube.video.service.BarrageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

/**
 * 弹幕表现层
 * @author ccr
 * @version 1.1
 */
@RestController
@CrossOrigin
@RequestMapping("/barrage")
public class BarrageController {
    @Autowired
    private BarrageService barrageService;

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Barrage> barrageList = barrageService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", barrageList);
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) {
        Barrage barrage = barrageService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", barrage);
    }


    /***
     * 新增数据
     * @param barrage
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Barrage barrage) {
        barrageService.add(barrage);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /***
     * 修改数据
     * @param barrage
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Barrage barrage, @PathVariable Long id) {
        barrage.setId(id);
        barrageService.update(barrage);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        barrageService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<Barrage> list = barrageService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Barrage> pageList = barrageService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/video/{videoId}")
    public Result findListByVideoId(@PathVariable("videoId") String videoId) {

        List<Barrage> barrageList = barrageService.findList(videoId);

//        List<Map> result = new ArrayList();
//
//
//        for (Barrage barrage : barrageList) {
//            HashMap map = new HashMap();
//            map.put("text",barrage.getContent());
//            map.put("color",barrage.getColor());
//            map.put("size",barrage.getSize());
//            map.put("position",barrage.getPosition());
//            map.put("time",barrage.getTime());
//            result.add(map);
//        }
//        System.out.println(JSON.toJSONString(result));

        return new Result(true,StatusCode.OK,"查询成功",barrageList);
    }

    @GetMapping("/video/web/{videoId}")
    public Result findListByVideoIdWeb(@PathVariable("videoId") String videoId){
        List<Barrage> barrageList = barrageService.findList(videoId);

//        List<Map> result = new ArrayList();
//
//
//        for (Barrage barrage : barrageList) {
//            HashMap map = new HashMap();
//            map.put("text",barrage.getContent());
//            map.put("color",barrage.getColor());
//            map.put("size",barrage.getSize());
//            map.put("position",barrage.getPosition());
//            map.put("time",barrage.getTime());
//            result.add(map);
//        }

        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");

        for (Barrage barrage : barrageList) {
            Date barrageTime = barrage.getBarrageTime();
            String format = sdf.format(barrageTime);
            barrage.setFormat(format);
        }

        return new Result(true,StatusCode.OK,"查询成功",barrageList);
    }

}
